﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace NetFramework.Utility
{
    public static class WebsiteHelper
    {
        /// <summary>
        /// 获取客户端 IP 地址
        /// </summary>
        /// <returns></returns>
        public static string GetClientIPAddress()
        {
            string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (string.IsNullOrEmpty(ip))
            {
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                if (string.IsNullOrEmpty(ip))
                {
                    ip = HttpContext.Current.Request.UserHostAddress;
                }
            }
            else
            {
                // 取逗号分隔第一个 IP 为客户端IP
                string[] tmp = ip.Split(new char[] { ',' });
                ip = tmp[0];
            }

            return ip;
        }
    }
}
